package com.xiaoxin.experience.util;

import java.net.URL;
import java.net.URLEncoder;
import java.util.Objects;
import java.util.Optional;

/**
 * @author xiaoxin
 */
public class UrlUtil {

    private UrlUtil() {}

    public static String encode(String text)
    {
        try
        {
            return URLEncoder.encode(text,"utf-8");
        }
        catch (Exception e)
        {
            return text;
        }
    }

    public static boolean isUrl(String address)
    {
        try
        {
            URL url = new URL(address);
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }

    public static Optional<URL> getUrl(String address)
    {
        try
        {
            return Optional.of(new URL(address));
        }
        catch (Exception e)
        {
            return Optional.empty();
        }
    }

    public static String website(String address)
    {
        Optional<URL> urlOptional = getUrl(address);
        if (urlOptional.isPresent())
        {
            URL url = urlOptional.get();
            return website(url);
        }
        return null;
    }

    public static String website(URL url)
    {
        if (Objects.isNull(url))
        {
            return null;
        }
        String authority = url.getAuthority();
        String protocol = url.getProtocol();
        return protocol + "://" + authority;
    }

    public static String relativeURL(String address)
    {
        Optional<URL> urlOptional = getUrl(address);
        if (urlOptional.isPresent())
        {
            URL url = urlOptional.get();
            return relativeURL(url);
        }
        return null;
    }

    public static String relativeURL(URL url)
    {
        if (Objects.isNull(url))
        {
            return null;
        }
        String path = url.getPath();
        String relativePath = path.substring(0,path.lastIndexOf('/') + 1);
        return website(url) + relativePath;
    }

    public static String urlToHttps(String address, String port)
    {
        Optional<URL> optional = getUrl(address);
        if(optional.isPresent())
        {
            URL url = optional.get();
            String path = url.getPath();
            port = StringUtil.isNotBlank(port) ? port : url.getPort() + "";
            return "https://" + url.getHost() + ":" + port + path + "?" + url.getQuery();
        }
        return address;
    }

    public static String httpUrlToHttps(String url,String port)
    {
        if (url.startsWith("http"))
        {
            if (url.startsWith("https"))
            {
                return url;
            }

            String ipPortApi = url.substring(url.indexOf("//") + 2);

            if (ipPortApi.contains("/"))
            {
                String api = ipPortApi.substring(ipPortApi.indexOf("/"));
                if (ipPortApi.contains(":"))
                {
                    String ip = ipPortApi.split(":")[0];
                    return "https://" + ip + ":" + port + api;
                }
                String ip = ipPortApi.substring(0,ipPortApi.indexOf("/"));
                {
                    return "https://" + ip + ":" + port + api;
                }
            }
            return "https://" + ipPortApi + ":" + port;
        }
        return url;
    }
}
